import {
  methods,
  request,
} from '@/utils/request';//设置自己的request路径

const actions = {
  getfileUpload: {
    //填写回执带附件
    name: 'getfileUpload',
    url: '/api/freshSystem/student/receiptAttachedFile'
  },
  createParent: {
    //填写回执带附件
    name: 'createParent',
    url: '/api/activity/createparent'
  },
  setNewMessage: {
    name: 'setNewMessage',
    url: '/api/summercamp/notice/build',//后端接口
  },
  takePartIn: {
    name: 'takePartIn',
    url: '/api/activity/register',//后端接口
  },
  createSub: {
    name: 'createSub',
    url: '/api/activity/createsub'
  },
};
const customRequest = (
  action: any,
  token: string,
  formdata: any,
) => {
  const options: any = {
    headers: {
      // 'Content-Type': 'multipart/form-data',
      Accept: 'application/json',
      Authorization: localStorage.getItem("token") ? localStorage.getItem("token") : '',
    }
  };
  let url = action.url;
  switch (action.name) {
    case actions.getfileUpload.name:
      options['method'] = methods.post;
      options['body'] = formdata;
      break;
    case actions.createParent.name:
      options['method'] = methods.post;
      options['body'] = formdata;
      break;
    case actions.setNewMessage.name:
      console.log(formdata)
      let data = new FormData();
      data.append('title', (formdata.title))
      data.append('content', (formdata.content))
      data.append('teacherCode', (formdata.teacherCode))
      data.append('clusterType', (formdata.clusterType))
      data.append('label', (formdata.label))
      data.append('isFile', (formdata.isFile))
      data.append('form', JSON.stringify(formdata.form))
      if (formdata.upload) data.append('upload', formdata.upload.originFileObj)
      options['method'] = methods.post;
      options['body'] = data;
      break;
    case actions.takePartIn.name:
      let dataTake = new FormData();
      dataTake.append('activityId', (formdata.activityId))
      if (formdata.upload) dataTake.append('upload', formdata.upload)
      options['method'] = methods.post;
      options['body'] = dataTake;
      break;
    case actions.createSub.name:
      options['method'] = methods.post;
      options['body'] = formdata;
      break;
    default:
  }
  return request(url, options);
};

export function getfileUpload(token: any, formdata: any) {
  return customRequest(actions.getfileUpload, token, formdata);
}

export function createParent(token: any, formdata: any) {
  return customRequest(actions.createParent, token, formdata);
}
export function setNewMessage(token: any, formdata: any) {
  return customRequest(actions.setNewMessage, token, formdata);
}
export function takePartIn(token: any, formdata: any) {
  return customRequest(actions.takePartIn, token, formdata);
}
export function createSub(token: any, formdata: any) {
  return customRequest(actions.createSub, token, formdata);
}
